package com.epam.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.action.Action;
import com.epam.action.ActionFactory;

public class ElectiveCoursesServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected ActionFactory factory = new ActionFactory();

	public ElectiveCoursesServlet() {
		super();
	}

	protected String getActionName(HttpServletRequest request) {
		String path = request.getServletPath();
		return path.substring(1, path.lastIndexOf("."));
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Action action = factory.create(getActionName(request));
		String url = action.perform(request, response);
		if (url != null)
			getServletContext().getRequestDispatcher(url).forward(request,
					response);

	}
}
